package ru.agiledev.aos.frontend.client.panel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.*;
import ru.agiledev.aos.frontend.shared.TransformationResult;

import java.util.Iterator;
import java.util.List;

/**
 * Created by a.kirillov
 * Date: 2/24/12 Time: 6:22 PM
 */
public class TransformationResultDialog extends DialogBox {

    TransformResultsPanel transformResultsPanel = new TransformResultsPanel();

    public TransformationResultDialog(){
        setText("Статистика по трансформации запроса");
        VerticalPanel verticalPanel = new VerticalPanel();
        add(verticalPanel);

        ScrollPanel main = new ScrollPanel();
        main.setHeight("394px");
        main.setWidth("492px");

        verticalPanel.add(main);

        main.add(transformResultsPanel);

        HorizontalPanel bottom = new HorizontalPanel();
        verticalPanel.add(bottom);
        bottom.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);
        Button close = new Button("Закрыть");
        close.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                hideDialog();
            }
        });
        bottom.add(close);
        setModal(true);
    }

    public TransformResultsPanel getTransformResultsPanel() {
        return transformResultsPanel;
    }

    private void hideDialog(){
        hide();
    }

    class TransformResultsPanel extends VerticalPanel {
        /*
         _________________________________
        |Объекты интереса, тип запроса    |
        |_________________________________|
        |Сгенерированные запросы          |
        |_________________________________|

         */
        HorizontalPanel typePanel = new HorizontalPanel();
        HorizontalPanel objectsPanel = new HorizontalPanel();
        HorizontalPanel generatedQueriesPanel = new HorizontalPanel();

        //Labels
        HTML distinctiveObject, firstObject, secondObject, additionalObject, dateObject, queryType, generatedQueries;

        public TransformResultsPanel(){
            setStyleName("transformPanel");
            setHorizontalAlignment(VerticalPanel.ALIGN_LEFT);
            setSpacing(2);
            //Objects and type
            add(new HTML("<b><u>Тип запроса:</u></b>&nbsp"));
            queryType = new HTML();
            typePanel.add(queryType);
            typePanel.setStyleName("transformPanelEntry");

            add(typePanel);
            add(new HTML("<b><u>Объекты интереса:</u></b>"));
            objectsPanel.setStyleName("transformPanelEntry");


            distinctiveObject = new HTML();
            firstObject = new HTML();
            secondObject = new HTML();
            additionalObject = new HTML();
            dateObject = new HTML();
            objectsPanel.add(distinctiveObject);
            objectsPanel.add(firstObject);
            objectsPanel.add(secondObject);
            objectsPanel.add(additionalObject);
            objectsPanel.add(dateObject);

            add(objectsPanel);

            add(new HTML("<b><u>Сгенерированные запросы:</u></b>&nbsp"));
            generatedQueries = new HTML();
            generatedQueriesPanel.add(generatedQueries);
            //Generated queries
            generatedQueriesPanel.setStyleName("transformPanelEntry");
            generatedQueries.setHorizontalAlignment(HorizontalPanel.ALIGN_JUSTIFY);
            add(generatedQueriesPanel);
        }

        public void clearResults(){
            queryType.setHTML("");
            distinctiveObject.setHTML("");
            firstObject.setHTML("");
            secondObject.setHTML("");
            additionalObject.setHTML("");
            dateObject.setHTML("");

                generatedQueries.setHTML("");
        }

        public void setResults(TransformationResult tResult){
            if(tResult.getQuestionType()!=null){
                queryType.setHTML(tResult.getQuestionType());
            } else {
                queryType.setHTML("не определён");
            }

            if(tResult.getDistinctiveObject()!=null){
                if(tResult.getFirstObject()!=null){
                    distinctiveObject.setHTML(tResult.getDistinctiveObject()+",&nbsp");
                } else {
                    distinctiveObject.setHTML(tResult.getDistinctiveObject());
                }
            } else {
                distinctiveObject.setHTML(tResult.getInitialQuery().replaceAll("\\s+", ",&nbsp"));
                return;
            }

            if(tResult.getFirstObject()!=null){
                if((tResult.getSecondObject()!=null)||(tResult.getAdditionalObject()!=null)||(tResult.getDateObject()!=null)){
                    firstObject.setHTML(tResult.getFirstObject()+",&nbsp");
                } else {
                    firstObject.setHTML(tResult.getFirstObject());
                }
            }

            if(tResult.getSecondObject()!=null){
                if((tResult.getAdditionalObject()!=null)||(tResult.getDateObject()!=null)){
                    secondObject.setHTML(tResult.getSecondObject()+",&nbsp");
                } else {
                    secondObject.setHTML(tResult.getSecondObject());
                }
            }

            if(tResult.getAdditionalObject()!=null){
                if(tResult.getDateObject()!=null){
                    additionalObject.setHTML(tResult.getAdditionalObject()+",&nbsp");
                } else {
                    additionalObject.setHTML(tResult.getAdditionalObject());
                }
            }

            if(tResult.getDateObject()!=null){
                additionalObject.setHTML(tResult.getDateObject());
            }


            //Displaying list of generated queries in case they're not null
            List<String> queries = tResult.getQueries();

            if((queries!=null)&&(queries.size()>0)){
                String resQueries = "";
                for (Iterator<String> iterator = queries.iterator(); iterator.hasNext(); ) {
                    String next = iterator.next();
                    resQueries+=next;

                    if(iterator.hasNext()){
                        resQueries+=", ";
                    }
                }
                generatedQueries.setHTML(resQueries);
            }
        }
    }
}
